package org.imlinker.plugin.say;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

import org.imlinker.plugin.Say;
import org.imlinker.util.StringUtils;

@SuppressWarnings("serial")
public class FollowDialog extends JDialog {

	private Say say;

	private DefaultListModel listModel = new DefaultListModel();

	public FollowDialog(final java.awt.Frame parent, final boolean modal,
			Say say) {
		super(parent, modal);

		this.say = say;

		initComponents();
		initValues();
		initActions();

		this.pack();
		this.setLocationRelativeTo(null);
	}

	private void initValues() {
		jList1.setModel(listModel);
	}

	private void initActions() {
		jButton2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// add
				int s = jList1.getSelectedIndex();
				String user;
				if (s < 0) {
					user = JOptionPane.showInputDialog("Input name:", "");
				} else {
					user = (String) jList1.getSelectedValue();
				}
				if (StringUtils.isEmpty(user))
					return;

				int ret = JOptionPane.showConfirmDialog(FollowDialog.this,
						"Add " + user);
				if (ret == JOptionPane.OK_OPTION) {
					say.addBuddy(user);
				}
			}

		});

		jButton1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				jButton1.setEnabled(false);
				jButton1.setText("Getting...");

				new Thread(){
					public void run(){
						listModel.clear();
						ArrayList<String> recommandList = say
								.getRecommandList();
						if (recommandList == null) {
							return;
						}

						for (String name : recommandList) {
							listModel.addElement(name);
						}
						
						jButton1.setText("Get list");
						jButton1.setEnabled(true);
					}
				}.start();
			}

		});
	}

	// <editor-fold defaultstate="collapsed" desc="Generated Code">

	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jList1 = new javax.swing.JList();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();

		jScrollPane1.setViewportView(jList1);

		jButton1.setText("Get list");

		jButton2.setText("Add");

		Container container = this.getContentPane();
		GroupLayout layout = new GroupLayout(container);
		container.setLayout(layout);

		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												jScrollPane1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												194,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jButton2,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																92,
																Short.MAX_VALUE)
														.addComponent(
																jButton1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																92,
																Short.MAX_VALUE))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jScrollPane1,
																javax.swing.GroupLayout.Alignment.TRAILING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																276,
																Short.MAX_VALUE)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jButton2)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jButton1)))
										.addContainerGap()));
	}// </editor-fold>

	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JList jList1;
	private javax.swing.JScrollPane jScrollPane1;
	// End of variables declaration
}
